<template>
	<view>
		<view class="cell">
			<view class="label">手机号</view>
			<input type="number" maxlength="11" v-model="form.mobile" class="input" placeholder="请输入手机号" />
			<button @click="getCode" :disabled="codeDisabled" class="btn-code">{{ codeText }}</button>
		</view>

		<view class="cell">
			<view class="label">验证码</view>
			<input v-model="form.code" maxlength="6" class="input" type="text" placeholder="请输入验证码" />
		</view>

		<view class="tips">需要先使用微信登录，在【我的】-【设置】中绑定手机号后，才能使用手机号登录</view>

		<view class="fixed-wrap">
			<button @click="login()" :disabled="loginDisabled" class="btn-login">登录</button>
		</view>
	</view>
</template>

<script>
	import { isMobile } from '@/utils/validator.js';

	const SECONDS = 60;

	export default {
		data() {
			return {
				form: {
					mobile: '',
					code: '',
					invite_code:""
				},

				seconds: SECONDS,
				codeText: '获取验证码',
				counting: false,
				timer: null,
			
				codeStatus: true, //禁止重复点击
				invite_code: '', //邀请码
			};
		},
		onLoad() {
			if(uni.getStorageSync('inviteCode')) {
				this.invite_code = uni.getStorageSync('inviteCode');
			}
		},
		computed: {
			loginDisabled() {
				return !(this.form.mobile && this.form.code);
			},
			codeDisabled() {
				return !(this.form.mobile && !this.counting);
			}
		},
		methods: {
			// 登录
			loginHandle() {
			
			},

			// 获取验证码
			getCode() {
				if (!isMobile(this.form.mobile)) {
					return uni.showToast({
						title: '手机号码有误',
						icon: 'none'
					});
				}
				this.countdown();
			},

			// 倒计时
			countdown() {
				this.counting = true;
				this.codeText = this.seconds + ' s';
				this.timer = setInterval(() => {
					if (this.seconds > 0) {
						this.seconds--;
						this.codeText = this.seconds + ' s';
					} else {
						this.counting = false;
						this.codeText = '再次获取';
						this.seconds = SECONDS;
						clearInterval(this.timer);
						this.timer = null;
					}
				}, 1000);
			},
			login() {
				if (!isMobile(this.form.mobile)) {
					return uni.showToast({
						title: '手机号码有误',
						icon: 'none'
					});
				}
				
				console.log(777777777)
				
				var _this = this;
				 if (!this.form.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
				}else {

					this.$api.request('/api/user/mobilelogin2', this.form, uni.getStorageSync('token'), function(res){
						console.log(res);
						if(res.code == 1) {
							console.log( res.data.userinfo,6666666)
							 uni.setStorageSync('userinfo', res.data.userinfo);
							 uni.setStorageSync('token',  res.data.userinfo.token);
							uni.showToast({
								title: "绑定成功",
								icon: 'none'
							})
							
							setTimeout(function() {
								uni.switchTab({
									url: '/pages/index/index'
								});
							}, 500);
						}else{
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					});
				}
			}
		}
	};
</script>
<style lang="less">
	.cell {
		display: flex;
		align-items: center;
		padding: 16rpx 32rpx;
		border-bottom: 1px solid #f3f3f3;

		.label {
			font-size: 32rpx;
			font-weight: 600;
		}

		.input {
			flex: 1;
			height: 100rpx;
			padding: 0 32rpx;
		}

		.btn-code {
			font-size: 28rpx;
			background: #333;
			color: #fff;
			width: 200rpx;
		}

		.input-placeholder {
			color: #ccc;
		}
	}

	.tips {
		font-size: 26rpx;
		color: #999;
		padding: 64rpx 32rpx;
	}

	.fixed-wrap {
		position: fixed;
		left: 48rpx;
		right: 48rpx;
		bottom: 100rpx;
		.btn-login {
			background: #333;
			color: #fff;
			font-weight: bold;
			height: 100rpx;
			line-height: 100rpx;
		}
	}

	button[disabled].btn-login {
		background: #e6e6e6;
	}

	button[disabled].btn-code {
		background: #e6e6e6;
	}

	button::after {
		border: none;
	}
</style>
